package com.haiheng.voiceandbook.BookRepository

import android.util.Log
import com.haiheng.voiceandbook.Constants
import com.haiheng.voiceandbook.api.ApiService
import com.haiheng.voiceandbook.api.DownLoadService
import com.haiheng.voiceandbook.commo.CategoriesSettingDataDto
import com.haiheng.voiceandbook.common.*
import com.haiheng.voiceandbook.utils.ControlledRunner
import com.haiheng.voiceandbook.utils.SingleRunner
import okhttp3.ResponseBody
import kotlin.math.sin

class BookRepository {
    private val TAG = "BookRepository"

    //开始新的任务之前，等待之前的排序任务完成
    val singleRunner = SingleRunner()


    //开始新任务之前，如果前面有任务就取消前面的任务
    var controlledRunner = ControlledRunner<ResponseBody>()


    //开始新任务之前，如果前面有任务就取消前面的任务
    var searchRunner = ControlledRunner<BaseDto<BookMainDto>>()

    suspend fun getSettingCategories() : BaseDto<CategoriesSettingDataDto>{
        return singleRunner.afterPrevious {
            ApiService.apiService.getSettingCategory(Constants.getLibraryCode())
        }
    }
    suspend fun categoryList():BaseDto<CategoriesSettingDataDto>{
       var dataDto =  ApiService.apiService.getSettingCategory(Constants.getLibraryCode())
        if (dataDto.data!!.ebookCategoriesV2 == null){
            // notte: 2021/11/23 0023 兼容旧版本
            dataDto.data!!.ebookCategoriesV2 = getOldCategories()
        }
        return singleRunner.afterPrevious {
            dataDto
        }
    }

    // 获取图书分类
    suspend fun getOldCategories(): List<CategoriesDto> {
        var newCategoriesList = mutableListOf<CategoriesDto>()
        var baseCategory = ApiService.apiService.categories()
        var settingCategoryDto = ApiService.apiService.getSettingCategory(Constants.getLibraryCode())
        if (settingCategoryDto?.data?.enable == 1) {
            var list = baseCategory?.data?.data
            var netCategoryList: List<String>
            netCategoryList = settingCategoryDto?.data?.ebookCategories!!
            list?.forEach{
                for (name in netCategoryList!!){
                    if (it.categoryName.equals(name)){
                        Log.e("test","需要保留的分类:"+name)
                        newCategoriesList.add(it)
                        break
                    }
                }
            }
            Log.e("test","最后的分类长度:"+newCategoriesList.size)
        }
        if (newCategoriesList.size > 0){
            baseCategory?.data?.data = newCategoriesList;
        }
        return newCategoriesList
    }

    // 通过分类获取书籍信息
    suspend fun booksByCategoryId(categoryId: Long, page: Int, rows: Int): BaseDto<BookMainDto> {

        return singleRunner.afterPrevious {
            ApiService.apiService.booksByCategoryId(categoryId, page, rows)

        }


    }

    /**
     * 获取书籍详细信息
     */
    suspend fun getBookDetail(bookId: Long): BaseDto<BookDetailDataDto> {
        return ApiService.apiService.getBookDetail(bookId,
            Constants.getLibraryCode(),
            Constants.getDevicesId())


    }


    suspend fun downLoadUrl(path: String?): ResponseBody {
        return  DownLoadService.downLoadService.downLoadUrl(path)

    }


    /**
     * 搜索书籍
     */
    suspend fun search(vo: SearchVo): BaseDto<BookMainDto> {

        return searchRunner.cancelPreviousThenRun {
            ApiService.apiService.search(vo)
        }

    }

    /**
     * 获取最新排行
     */
    suspend fun latestRank(page:Int,rows:Int): AmountDataDto {
//        return ApiService.apiService.latestRank(page,rows)
        return ApiService.apiService.recommend(page,rows)

    }

    /**
     * 获取热门排行
     */
    suspend fun hotRank(page:Int,rows:Int): AmountDataDto {
        return ApiService.apiService.hotRank(page,rows)

    }



    /**
     * 获取热门排行
     */
    suspend fun reportAlbumClick(albumId:String): BaseDto<Any> {
        return ApiService.apiService.reportAlbumClick(Constants.getLibraryCode()
            ,Constants.getDevicesId(),
            albumId)

    }

    //上报播放页面时长
    suspend fun reportPlay(albumId:String,timer:Long): BaseDto<Any> {
        return ApiService.apiService.reportPlay(Constants.getLibraryCode()
            ,Constants.getDevicesId(),
            albumId,
            timer.toString())
    }
    //用户点击开始阅读后，在播放页面生成定时器,退出后上报时长
    suspend fun reportReading(bookId:String,timer:Long): BaseDto<Any> {
        return ApiService.apiService.reportReading(Constants.getLibraryCode()
            ,Constants.getDevicesId(),
            bookId,
            timer.toString())
    }
    //阅读次数上报
    suspend fun reportReadTimes(bookId:String): BaseDto<Any> {
        return ApiService.apiService.reportReadTimes(Constants.getLibraryCode()
            ,Constants.getDevicesId(),
            bookId)
    }
}